package com.transaction.simulate.service.impl;

import com.transaction.common.base.User;
import com.transaction.common.util.CodeUtil;
import com.transaction.common.util.MD5Util;
import com.transaction.simulate.mapper.MenuMapper;
import com.transaction.simulate.mapper.UserMapper;
import com.transaction.simulate.model.dto.Menu;
import com.transaction.simulate.model.dto.Role;
import com.transaction.simulate.model.vo.MenuVo;
import com.transaction.simulate.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    private static Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 根据用户名查询登录用户
     * @param accountName
     * @return
     */
    @Override
    public User getUserByAccountName(String accountName) {
        return userMapper.getUserByAccountName(accountName);
    }

    /**
     * 根据用户Code查询菜单
     * @param menuVo
     * @return
     */
    @Override
    public MenuVo getUserMenu(MenuVo menuVo) {
        //查询所有的菜单
        List<Menu> menuList = menuMapper.selectAllMenuByUserCode(menuVo.getUserCode());

        List<Menu> menus = buildMenus(menuList);

        //组装所有的菜单请求和名称
        Map<String,String> menuMap = menuList.stream().collect(Collectors.toMap(Menu::getMenuUrl, Menu::getMenuName));

        menuVo.setMenus(menus);
        menuVo.setMenuMap(menuMap);
        return menuVo;
    }

    @Override
    public List<Menu> buildMenus(List<Menu> menuList) {
        //组装一级菜单
        List<Menu> menus = menuList.stream().filter(s -> Objects.isNull(s.getParentCode())).collect(Collectors.toList());
        //在一级菜单基础上组装二级菜单
        menus.forEach(p->{
            String menuCode = p.getMenuCode();
            List<Menu> children = new ArrayList<>();
            menuList.forEach(c->{
                if(Objects.equals(menuCode,c.getParentCode())){
                    children.add(c);
                }
            });
            p.setChildren(children);
        });

        return menus;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public String addUser(User user) {
        //查询用户名是否重复
        User userVo = getUserByAccountName(user.getAccountName());
        if(userVo != null){
            return "该用户名已存在,请更换.";
        }

        String salt = MD5Util.getSalt();
        user.setUserCode(CodeUtil.getCode());
        user.setSalt(salt);
        user.setPassWord(MD5Util.encryptedPassword(user.getAccountName(),user.getPassWord(),salt));
        user.setUserName(user.getAccountName());
        userMapper.addUser(user);
        return "注册成功";
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    public List<Role> getRoles() {
        return userMapper.getRoles();
    }
}
